﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class TaoBaoApi_TB_Config : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            Bind();
    }
    void Bind()
    {
        string sql = "select mid,appkey,appcertcode,isuse from tb_appcode where field1 is null";
        DataSet ds = DBUtility.SqlHelper.Query(DBUtility.SqlHelper.LocalSqlServer, sql);
        gvList.DataSource = ds.Tables[0];
        gvList.DataBind();
    }
    protected void gvList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvList.PageIndex = e.NewPageIndex;
        Bind();
    }
    protected void gvList_RowCreated(object sender, GridViewRowEventArgs e)
    {
        new TBCommon().gv_RowCreated(sender, e, gvList, Page);
    }
    protected string GetAppCodeStatus(object o)
    {
        if (o is DBNull || o == null || o.ToString().Length == 0)
            return null;
        return Convert.ToInt32(o) == 1 ? "<font color=red>启用</font>" : "未启用";
    }
    //启用
    protected void btnSave_Click(object sender, EventArgs e)
    {
        int c = 0;
        int mid = 0;
        foreach (GridViewRow row in gvList.Rows)
        {
            CheckBox chk = (CheckBox)row.FindControl("chkOne");
            if (chk != null && chk.Checked)
            {
                c++;
                mid = Convert.ToInt32(row.Cells[1].Text);
            }
        }
        if (c == 0)
        {
            new TBCommon().ShowMessage(Page, "请选择一条记录。");
            return;
        }
        if (c > 1)
        {
            new TBCommon().ShowMessage(Page, "一次只能启用一 个AppCode。");
            return;
        }
        if (mid > 0)
        {
            string sql = "update tb_appcode set isuse=0";
            DBUtility.SqlHelper.ExecuteSql(DBUtility.SqlHelper.LocalSqlServer, sql);
            sql = "update tb_appcode set isuse=1 where mid=" + mid;
            DBUtility.SqlHelper.ExecuteSql(DBUtility.SqlHelper.LocalSqlServer, sql);
        }
        Response.Redirect("tb_config.aspx");
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string appkey = txtAppKey.Text.TrimEnd();
        string appcertcode = txtAppCertCode.Text.TrimEnd();
        if (appkey.Length == 0 || appcertcode.Length == 0)
        {
            new TBCommon().ShowMessage(Page, "appkey和appcertcode都不允许为空。");
            return;
        }
        string sql = "insert into tb_appcode(appkey,appcertcode)values('" + appkey + "','" + appcertcode + "')";
        DBUtility.SqlHelper.ExecuteSql(DBUtility.SqlHelper.LocalSqlServer, sql);
        Response.Redirect("tb_config.aspx");
    }
}
